package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCartMapper;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.util.AdminResponseCode;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static java.time.LocalDateTime.now;

@Service
public class WxCouponServiceImpl implements WxCouponService {

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;


    @Override
    public List<MarketCoupon> list(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.or().andDeletedEqualTo(false);
        List<MarketCoupon> couponList = couponMapper.selectByExample(couponExample);
        return couponList;
    }

    @Override
    public List<MarketCoupon> mylist(Short status, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        couponUserExample.or().andStatusEqualTo(status).andDeletedEqualTo(false);
        List<MarketCouponUser> couponUserList = couponUserMapper.selectByExample(couponUserExample);
        List<MarketCoupon> resultList = new ArrayList<>();
        for (MarketCouponUser couponUser : couponUserList) {
            Integer couponId = couponUser.getCouponId();
            MarketCoupon coupon = couponMapper.selectByPrimaryKey(couponId);
            resultList.add(coupon);
        }
        return resultList;
    }


    @Autowired
    MarketCartMapper cartMapper;

    /**
     * 1-先根据cartId找到购物车里的商品
     * 2-将购物车的商品总价格加在一起, 看符不符合优惠券最低条件
     * 3-再与优惠券截止日期相比较
     *
     * @param userId
     * @return
     */
    @Override
    public List<MarketCoupon> selectlist(Integer userId) {
        // 1-先根据cartId找到购物车里的商品
        MarketCartExample cartExample = new MarketCartExample();
        cartExample.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<MarketCart> cartList = cartMapper.selectByExample(cartExample);

        // 遍历购物车, 汇总总金额
        BigDecimal resultPrice = new BigDecimal(0);
        for (MarketCart cart : cartList) {
            resultPrice = resultPrice.add(cart.getPrice());
        }

        // 找到优惠券列表中少于总金额的优惠券, 返回回来符合条件的优惠券
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.or().andMinLessThan(resultPrice).andDeletedEqualTo(false);
        PageHelper.startPage(1, 10);
        List<MarketCoupon> couponList = couponMapper.selectByExample(couponExample);
        return couponList;
    }



    /**
     * 1-根据优惠券id, 查找user_coupon表中,
     * 优惠券是否有这个优惠券, 有的话返回已领取
     * 2-没有的话再保存到user_coupon表
     *
     * @param couponId
     * @return
     */
    @Override
    public int receive(Integer userId,Integer couponId) {
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        couponUserExample.or().andCouponIdEqualTo(couponId).andUserIdEqualTo(userId);
        List<MarketCouponUser> list = couponUserMapper.selectByExample(couponUserExample);
        if (list.size() != 0) {
            return AdminResponseCode.COUPON_EXISTED;
        }
        // 2-没有的话再保存到user_coupon表
        MarketCouponUser couponUser = new MarketCouponUser();
        couponUser.setCouponId(couponId);
        couponUser.setStatus((short) 0);
        couponUser.setAddTime(now());
        couponUser.setUpdateTime(now());
        couponUser.setUserId(userId);
        couponUser.setDeleted(false);

        couponUserMapper.insertSelective(couponUser);
        return 200;

    }


    /**
     * 1-兑换优惠券, 先将优惠券的码接收
     * 2-与coupon表中的code列进行比对,如果存在, 提示领取成功, 然后将status置为3表示下架
     * 3-当你再次领取同一张优惠券时, 会判断status是否是3, 是3的话告诉你已使用
     * 4-TODO:是2告诉你已下架, 是1告诉你已过期
     * 5-是0的话将信息插入到coupon_user表
     * @param code
     * @return
     */
    @Override
    public int exchange(Integer userId,String code) {
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.or().andCodeEqualTo(code);
        MarketCoupon marketCoupon = couponMapper.selectOneByExample(couponExample);
        if(marketCoupon==null){
            return AdminResponseCode.COUPON_CODE_ERROR;
        }
        // 表示优惠券已使用
        if(marketCoupon.getStatus()==3){
            return AdminResponseCode.COUPON_USED;
        }
        // 表示正常可用
        if (marketCoupon.getStatus()==0){
            MarketCouponUser couponUser = new MarketCouponUser();
            couponUser.setUserId(userId);
            couponUser.setCouponId(marketCoupon.getId());
            couponUser.setStatus((short)0);
            couponUser.setAddTime(now());
            couponUser.setUpdateTime(now());
            couponUser.setDeleted(false);
            couponUserMapper.insertSelective(couponUser);
            return 200;
        }

        return 0;
    }


}
